package osb.map.map;

import java.util.HashMap;
import osb.map.main.ShareActivity;
import osb.map.net.ClientSocket;
import osb.map.net.DataGPS;
import osb.map.net.DataPackage;
import osb.map.net.msgType;
import osb.map.publicdata.Public_Operate_And_Data;
import android.R.integer;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.content.Intent;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.ljp.ani.R;

/**
 * 地图activity
 * @author Administrator
 *
 */

public class Map extends MapActivity
{
	static public Map Public_Static_Map;
	
	public BMapManager mapManager;
	public MapView mapView;
	public MapController mapController;
	LocationListener mLocationListener = null;
	MyLocationOverlay mLocationOverlay = null;

	public int i = 0;
	ListView list;

	// 加入listview的内容
	String data[] = new String[]
	{ "图书馆", "行政楼", "东区田径场", "西门", "南门", "立诚", "致广", "笃行", "知明", "音乐厅", "人文楼", "理工楼", "文化街", "学生公寓" };


	// contorl ---
	private boolean ShowMoreBottom = false;               // 是否显示更多按钮 false表示不现实 收缩
	private int CurrentTuceng = 0;                        // 当前图层 0表示交通地图 1表示卫星地图 默认为交通地图

	private ImageButton ImageButton_ShowMore_extren;      // 更多按钮
	private LinearLayout moreLinearLayout_extren;
	private LinearLayout moreLinearLayout_top_extren;
	private RelativeLayout Search_RelativeLayout_extren;  // 搜索框
	
	private boolean IsStartCeju = true;       // 是否打开测距
	private boolean IsStartTrack = false;     // 是否打开测距
	private boolean IsStartXiaolv = true;     // 是否打开小绿
	private boolean IsStartWodeweizhi = true; // 是否打开我的位置

	int extren_defaultSelectedStatus[] = { 0, 0, 0, 0, 0, 0, 0 };  // 轨迹跟踪
	
	public HashMap<String,GeoPoint> busLocationMap = new HashMap<String,GeoPoint>();
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		
		Public_Static_Map = this;
		
		mapManager = new BMapManager(getApplication());
		mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null);
		super.initMapActivity(mapManager);
		mapView = (MapView) findViewById(R.id.map_View);
		// 设置地图模式为交通地图
		mapView.setTraffic(true);
		// 设置启用内置的缩放控件
		mapView.setBuiltInZoomControls(false);
		// 移动
		mapView.setDrawOverlayWhenZooming(true);
		// 用给定的经纬度构造一个GeoPoint（纬度，经度）
		GeoPoint point = new GeoPoint((int) (26.035398 * 1E6), (int) (119.217864 * 1E6));
		// 取得地图控制器对象，用于控制MapView
		mapController = mapView.getController();
		// 设置地图的中心
		mapController.setCenter(point);
		// 设置地图默认的缩放级别
		mapController.setZoom(16);
		// 显示我的位置
		Map_MyLocation.UseLocationListener(); 
		
		
		// 搜索框
		final RelativeLayout Search_RelativeLayout = (RelativeLayout) findViewById(R.id.search);
		Search_RelativeLayout_extren = Search_RelativeLayout;
		// 弹出更多按钮选项
		final ImageButton ImageButton_ShowMore = (ImageButton) findViewById(R.id.ShowMore);
		ImageButton_ShowMore_extren = ImageButton_ShowMore; 
		
		
		ImageButton_ShowMore.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				// 弹出更多按钮选选项
				if (ShowMoreBottom)
				{
					LinearLayout MoreLinearLayout, MoreLinearLayout_top;
					MoreLinearLayout = (LinearLayout) findViewById(R.id.linearLayout2);
					MoreLinearLayout_top = (LinearLayout) findViewById(R.id.linearLayout_top);
					MoreLinearLayout.setVisibility(View.INVISIBLE); // 隐藏
					MoreLinearLayout_top.setVisibility(View.INVISIBLE);
					ShowMoreBottom = false;
					ImageButton_ShowMore.setImageDrawable(getResources().getDrawable(R.drawable.more_open)); // 按钮图片变换
				} else
				{
					// 显示更多按钮
					LinearLayout MoreLinearLayout, MoreLinearLayout_top;
					MoreLinearLayout = (LinearLayout) findViewById(R.id.linearLayout2);
					MoreLinearLayout_top = (LinearLayout) findViewById(R.id.linearLayout_top);
					MoreLinearLayout.setVisibility(View.VISIBLE);
					MoreLinearLayout_top.setVisibility(View.VISIBLE);
					moreLinearLayout_extren = MoreLinearLayout;
					moreLinearLayout_top_extren = MoreLinearLayout_top;
					ShowMoreBottom = true;
					ImageButton_ShowMore.setImageDrawable(getResources().getDrawable(R.drawable.more_close)); // 按钮图片变换

					// 4大按钮 图层 测距 小绿 我位置 设置监听器
					final ImageButton Settuceng = (ImageButton) findViewById(R.id.tuceng);
					final ImageButton Setceju = (ImageButton) findViewById(R.id.ceju);
					final ImageButton Setxiaolv = (ImageButton) findViewById(R.id.xiaolv);
					final ImageButton Setwodeweizhi = (ImageButton) findViewById(R.id.wodeweizhi);
					final ImageButton Setyujing = (ImageButton) findViewById(R.id.yujing);
					// 图层按钮
					Settuceng.setOnClickListener(new OnClickListener()
					{
						public void onClick(View v)
						{ 
							// 图层按钮
							new AlertDialog.Builder(Map.this)
									.setTitle("图层选择")
									.setIcon(android.R.drawable.ic_dialog_info)
									.setSingleChoiceItems(
											new String[]
											{ "交通视图", "卫星视图" },
											CurrentTuceng,
											new DialogInterface.OnClickListener()
											{
												public void onClick( DialogInterface dialog, int which)
												{
													if (which == 0) // 交通地图模式
													{
														mapView.setTraffic(true);
														mapView.setSatellite(false);
														CurrentTuceng = 0; 
													}
													if (which == 1) // 卫星地图模式
													{
														mapView.setTraffic(false);
														mapView.setSatellite(true);
														CurrentTuceng = 1; 
													}
													dialog.dismiss();
												}
											}).show();
						}
					});
					// 测距按钮
					Setceju.setOnClickListener(new OnClickListener()
					{
						public void onClick(View v)
						{
							// 测距按钮
							if (IsStartCeju)
							{
								Map_Ceju.StartCeJu();
								Setceju.setImageResource(R.drawable.ceju1);
								IsStartCeju = false;
							} else
							{
								Map_Ceju.CloseCeJu();
								Setceju.setImageResource(R.drawable.ceju);
								IsStartCeju = true;
							}
						}
					});
					// 小绿位置
					Setxiaolv.setOnClickListener(new OnClickListener()
					{
						public void onClick(View v)
						{ 
							//开启小绿位置监听
							if (IsStartXiaolv)
							{
								// 发送 请求小绿位置
								DataPackage data = new DataPackage(msgType.Bus_Local_Ask);
								ClientSocket.Public_Static_ClientSocket.sendMsg(data);
								Setxiaolv.setImageResource(R.drawable.xiaolv1);
								IsStartXiaolv = false;
							}
							//关闭小绿位置监听
							else
							{
								DataPackage data = new DataPackage(msgType.Bus_Local_AskStop);
								ClientSocket.Public_Static_ClientSocket.sendMsg(data);
								Setxiaolv.setImageResource(R.drawable.xiaolv);
								Map_ItemizedOverlay.CloseGetxiaolvlocation();//清除所有图层
								IsStartXiaolv = true;
							}

						}
					});
					// 我的位置
					Setwodeweizhi.setOnClickListener(new OnClickListener()
					{
						public void onClick(View v)
						{
							// 关闭位置
							if (IsStartWodeweizhi)     
							{
								Map_MyLocation.CloseLocationListener();
								Setwodeweizhi.setImageResource(R.drawable.wodeweizhi);
								IsStartWodeweizhi = false;
							} else
							{
								Setwodeweizhi.setImageResource(R.drawable.wodeweizhi1);
								IsStartWodeweizhi = true;
								Map_MyLocation.UseLocationListener();
							}
						}
					});
					// 轨迹跟踪功能
					Setyujing.setOnClickListener(new OnClickListener()
					{
						public void onClick(View v)
						{
							IsStartTrack = false;
							final boolean[] defaultSelectedStatus = { false, false, false, false, false, false, false };

							for (int j = 0; j < 7; j++)
							{
								if (extren_defaultSelectedStatus[j] == 1)
								{
									defaultSelectedStatus[j] = true;

								}
							}
							
							new AlertDialog.Builder(Map.this)
									.setTitle("轨迹记录")
									.setMultiChoiceItems(
											new String[]
											{ "记录我的轨迹", "记录小绿1的轨迹", "记录小绿2的轨迹", "记录小绿3的轨迹", "记录小绿4的轨迹", "记录小绿5的轨迹", "记录小绿6的轨迹" },
											defaultSelectedStatus,
											new OnMultiChoiceClickListener()
											{
												public void onClick( DialogInterface dialog, int which, boolean isChecked)
												{
													// 来回重复选择取消，得相应去改变item对应的bool值，点击确定时，根据这个bool[],得到选择的内容
													defaultSelectedStatus[which] = isChecked;
												}

											})
									.setPositiveButton(
											"确定",
											new DialogInterface.OnClickListener()
											{
												public void onClick( DialogInterface dialoginterface, int i)
												{
													for (int j = 0; j < defaultSelectedStatus.length; j++)
													{
														if (defaultSelectedStatus[j])
														{ 
															// 已被选择
															extren_defaultSelectedStatus[j] = 1;
															switch (j)
															{
															case 0:
																Public_Operate_And_Data.OpenTrack_me = true;
																break;
															case 1:
																Public_Operate_And_Data.OpenTrack_xiaolv1 = true;
																break;
															case 2:
																Public_Operate_And_Data.OpenTrack_xiaolv2 = true;
																break;
															case 3:
																Public_Operate_And_Data.OpenTrack_xiaolv3 = true;
																break;
															case 4:
																Public_Operate_And_Data.OpenTrack_xiaolv4 = true;
																break;
															case 5:
																Public_Operate_And_Data.OpenTrack_xiaolv5 = true;
																break;
															case 6:
																Public_Operate_And_Data.OpenTrack_xiaolv6 = true;
																break;

															default:
																break;
															}
															IsStartTrack = true;
														} else
														{ 
															// 反选关闭
															extren_defaultSelectedStatus[j] = 0;
															switch (j)
															{
															case 0:
																Public_Operate_And_Data.OpenTrack_me = false;
																break;
															case 1:
																Public_Operate_And_Data.OpenTrack_xiaolv1 = false;
																
																Map_Track.RemoveTrack(1);
																break;
															case 2:
																Public_Operate_And_Data.OpenTrack_xiaolv2 = false;

																Map_Track.RemoveTrack(2);
																break;
															case 3:
																Public_Operate_And_Data.OpenTrack_xiaolv3 = false;

																Map_Track.RemoveTrack(3);
																break;
															case 4:
																Public_Operate_And_Data.OpenTrack_xiaolv4 = false;

																Map_Track.RemoveTrack(4);
																break;
															case 5:
																Public_Operate_And_Data.OpenTrack_xiaolv5 = false;

																Map_Track.RemoveTrack(5);
																break;
															case 6:
																Public_Operate_And_Data.OpenTrack_xiaolv6 = false;

																Map_Track.RemoveTrack(6);
																break;

															default:
																break;
															}
														}
													}
													if (IsStartTrack)
														Setyujing.setImageResource(R.drawable.yujing1);
													else
													{
														Setyujing.setImageResource(R.drawable.yujing);
													}
												}
											}).setNegativeButton("取消", null)
									.show();
						}
					});
				}
			}

		});


		// 搜索框
		final Button search_button1 = (Button) findViewById(R.id.btm_search);
//		final Button search_button2 = (Button) findViewById(R.id.btm_zidingyi);
		// 关键字搜索
		search_button1.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v) 
			{
				final TextView search_edit = (TextView) findViewById(R.id.search_editText1);
				String string = search_edit.getText().toString();
				if (Public_Operate_And_Data.CurrentPoint != null)
					Map_MkSearch.GetSearch_appoint(string,Public_Operate_And_Data.CurrentPoint, 50000); // 例子
				else
				{
					Toast.makeText(Map.this, "无法获取您的位置", Toast.LENGTH_SHORT).show();
					return;
				}
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				if (imm.isActive())
				{
					imm.hideSoftInputFromWindow(search_edit.getWindowToken(), 0);
				}

				mapView.invalidate();
				
				Toast.makeText(Map.this, "搜索范围:50000米  关键字:" + string,Toast.LENGTH_SHORT).show();
			}
		});

		Public_Operate_And_Data.Public_mapView = mapView; 
		Public_Operate_And_Data.Public_BMapManager = mapManager;
	}

	public void busLocalAskSuccess(DataGPS dataGPS)
	{
		if(mapManager == null)
			return;

		Map.Public_Static_Map.mapView.invalidate();

		String busIDString = "" + dataGPS.busID;
		// 终端退出
		if ((dataGPS.localX == -1) && (dataGPS.localY == -1)) {
			Map_Entity map_Entity = new Map_Entity();
			if (Map_Entity.static_Map_Entity.containsKey(busIDString)) {
				map_Entity = Map_Entity.static_Map_Entity.get(busIDString);
				Map_ItemizedOverlay.RemoveMap_ItemizedOverlay(map_Entity);
			}
		} 
		else {
			GeoPoint p1 = new GeoPoint((int) (dataGPS.localX * 1E6),(int) (dataGPS.localY * 1E6));
			Map.Public_Static_Map.mapView.invalidate();
			
			
			// 轨迹跟踪处理
			switch (dataGPS.busID) {
			case 1:
				// 我的轨迹跟踪功能已经打开
				if (Public_Operate_And_Data.OpenTrack_xiaolv1) {
					// 把我最新的位置传进去处理
					Map_Track.OpenTrack(1, p1);
				}
				break;
			case 2:
				if (Public_Operate_And_Data.OpenTrack_xiaolv2) {
					Map_Track.OpenTrack(2, p1);
				}
				break;
			case 3:
				if (Public_Operate_And_Data.OpenTrack_xiaolv3) {
					Map_Track.OpenTrack(3, p1);
				}
				break;
			case 4:
				if (Public_Operate_And_Data.OpenTrack_xiaolv4) {
					Map_Track.OpenTrack(4, p1);
				}
				break;
			case 5:
				if (Public_Operate_And_Data.OpenTrack_xiaolv5) {
					Map_Track.OpenTrack(5, p1);
				}
				break;
			case 6:
				if (Public_Operate_And_Data.OpenTrack_xiaolv6) {
					Map_Track.OpenTrack(6, p1);
				}
				break;
			default:
				break;
			}

			Map_Entity map_Entity;
			if (Map_Entity.static_Map_Entity.containsKey(busIDString)) {
				Map_Entity.static_Map_Entity.get(busIDString).geoPoint = p1;
				map_Entity = Map_Entity.static_Map_Entity.get(busIDString);
			}

			else {
				map_Entity = new Map_Entity();
				map_Entity.busID = busIDString;
				map_Entity.geoPoint = p1;
				map_Entity.drawable = null;
				map_Entity.map_ItemizedOverlay = null;
				Map_Entity.static_Map_Entity.put(busIDString, map_Entity);
			}

			int busIDInt = Integer.parseInt(busIDString);
			switch (busIDInt) {
			case 1:
				map_Entity.drawable = getResources().getDrawable(R.drawable.bus_1);
				Map_ItemizedOverlay.ShowMap_ItemizedOverlay(null, map_Entity);

				break;
			case 2:
				map_Entity.drawable = getResources().getDrawable(R.drawable.bus_2);
				Map_ItemizedOverlay.ShowMap_ItemizedOverlay(null, map_Entity);

				break;
			case 3:
				map_Entity.drawable = getResources().getDrawable(R.drawable.bus_3);
				Map_ItemizedOverlay.ShowMap_ItemizedOverlay(null, map_Entity);

				break;
			case 4:
				map_Entity.drawable = getResources().getDrawable(R.drawable.bus_4);
				Map_ItemizedOverlay.ShowMap_ItemizedOverlay(null, map_Entity);

				break;
			case 5:
				map_Entity.drawable = getResources().getDrawable(
						R.drawable.bus_5);
				Map_ItemizedOverlay.ShowMap_ItemizedOverlay(null, map_Entity);

				break;
			case 6:
				map_Entity.drawable = getResources().getDrawable(
						R.drawable.bus_6);
				Map_ItemizedOverlay.ShowMap_ItemizedOverlay(null, map_Entity);

				break;
			default:
				break;

			}
		}
		
	}

	@Override
	protected boolean isRouteDisplayed()
	{
		return false;
	}

	@Override
	protected void onDestroy()
	{

		Public_Operate_And_Data.OpenTrack_xiaolv1 = false;
		Public_Operate_And_Data.OpenTrack_xiaolv2 = false;
		Public_Operate_And_Data.OpenTrack_xiaolv3 = false;
		Public_Operate_And_Data.OpenTrack_xiaolv4 = false;
		Public_Operate_And_Data.OpenTrack_xiaolv5 = false;
		Public_Operate_And_Data.OpenTrack_xiaolv6 = false;
		
		DataPackage data = new DataPackage(msgType.Bus_Local_AskStop);
		ClientSocket.Public_Static_ClientSocket.sendMsg(data);
		Map_ItemizedOverlay.CloseGetxiaolvlocation();//清除所有图层
		IsStartXiaolv = true;
		
		for(int i = 0 ; i < 6 ; i ++)
		{
		    Map_Track.RemoveTrack(i);
		}
		if (mapManager != null)
		{
			mapManager.destroy();
			mapManager = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause()
	{
		if (mapManager != null)
		{
			mapManager.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume()
	{
		if (mapManager != null)
		{
			mapManager.start();
		}
		super.onResume();
	}

//	// 点击MENU 事件 执行全屏视图
	@Override
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		int v = this.getWindow().getAttributes().flags;
		// 全屏 66816 - 非全屏 65792
		// 全屏
		if (Public_Operate_And_Data.IsMapFullScreen)
		{
			Public_Operate_And_Data.IsMapFullScreen = false;
			// 取消全屏
						this.getWindow().clearFlags(
								WindowManager.LayoutParams.FLAG_FULLSCREEN);
						ImageButton_ShowMore_extren.setVisibility(View.VISIBLE); // 显示弹出更多按钮的按钮
						if (ShowMoreBottom)
						{
							moreLinearLayout_extren.setVisibility(View.VISIBLE); // 显示弹出更多按钮
							moreLinearLayout_top_extren.setVisibility(View.VISIBLE);
						}
						Search_RelativeLayout_extren.setVisibility(View.VISIBLE);
		} 
		else
		{
			Public_Operate_And_Data.IsMapFullScreen = true;
			// 非全屏
						this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
						ImageButton_ShowMore_extren.setVisibility(View.INVISIBLE); // 隐藏弹出更多按钮的按钮
						Search_RelativeLayout_extren.setVisibility(View.INVISIBLE);
						if (ShowMoreBottom)
						{
							moreLinearLayout_extren.setVisibility(View.INVISIBLE); // 隐藏弹出更多按钮
							moreLinearLayout_top_extren.setVisibility(View.INVISIBLE);
						}
			
		}
//		 如果返回false，此方法就把用户点击menu的动作给消费了，onCreateOptionsMenu方法将不会被调用
		return true;
	}
	
	 /**
	 * menu
	 */
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		menu.add(Menu.NONE, Menu.FIRST + 1, 3, "分享下载").setIcon(android.R.drawable.ic_menu_send);
//		return true;
//
//	}
//
//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		switch (item.getItemId()) {
//		case Menu.FIRST + 1:
//			Intent intent = new Intent();
//			intent.setClass(Map.this, ShareActivity.class);
//			Map.this.startActivity(intent);
//
//			break;
//		}
//		return false;
//	}
	 
}